package com.glela.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.api.im.rongcloud.ApiHttpClient;
import com.glela.api.im.rongcloud.vo.SdkHttpResult;
import com.glela.common.beans.FormatType;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.request.RequestSendMessageModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping("/sendMessage")
public class SendMessageController {
	
	
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	
	
	@RequestMapping(value = "/rongyun", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> rongyun(@RequestBody RequestSendMessageModel model, HttpServletRequest request) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(), model.getNickName(),model.getUrlhander())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		
		String userId = model.getUserId();
		String nickName = model.getNickName();
		String urlhander = model.getUrlhander();
		SdkHttpResult result = null;
		
		//双十一临时处理方案,通过配置
		BaseRestrictions baseRestrictions = baseRestrictionsService.getBaseRestrictionsForCache("RONG_CLOUD_CREATE_TOKEN");
		if (null != baseRestrictions) {
			if (baseRestrictions.getStatus().toString().equals("0")) {//禁用
				result = new SdkHttpResult(200, "");//返回空字符串
			} else {
//				sdkHttpResult = ApiHttpClient.getToken(String.valueOf(user.getId()), user.getUserName(),						user.getPicture(), FormatType.json);
				result = ApiHttpClient.getToken(userId, nickName,urlhander, FormatType.json);
			}
		} else {
			//sdkHttpResult = ApiHttpClient.getToken(String.valueOf(user.getId()), user.getUserName(),					user.getPicture(), FormatType.json);
			result = ApiHttpClient.getToken(userId, nickName,urlhander, FormatType.json);
		}
		
		
		return new ResponseEntity<>(new ResponseResultModel<SdkHttpResult>(result), HttpStatus.OK);
	}

}
